package com.newblog.test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class PropertyFile {
	public String readProperty(String file,String key){
		Properties pro = new Properties();	
		String value = null;
		try{
		   InputStream input  = new FileInputStream(file);
		   pro.load(input);
		   value =  (String)pro.get(key);
		}
		catch(IOException e){
		 	e.printStackTrace();
		}
		return value;
	}
	public void writePropery(String file,String k,String v){
		Properties pro = new Properties();
		try{
			OutputStream output = new  FileOutputStream(file);
			pro.setProperty(k, v);
			pro.store(output, "");
		}
		catch(IOException e){
			e.printStackTrace();
		}
	}
    public Map readProperties(String file){
    	Map map = null;
    	Properties pro = new Properties();
    	InputStream input = null;
    	try{
    		input = new FileInputStream(file);
    		pro.load(input);
    		Enumeration enums =  pro.propertyNames();
    		while(enums.hasMoreElements()){
    			map = new HashMap();
    			String key = (String)enums.nextElement();
    			map.put(key, pro.get(key));
    		}
    	}
    	catch(IOException e){
    		e.printStackTrace();
    	}
    	return map;
    }
	
	public static void main(String[] args){
		PropertyFile pf = new PropertyFile();
		pf.writePropery("abc.properties", "abc", "hello");
		System.out.println(pf.readProperties("abc.properties"));
	}

}
